<!--饼图-->
<template>
  <div :id="id" :ref="id" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

const animationDuration = 1000
export default {
  name:"pieChart",
  mixins: [resize],
  props: {
    id: {
      type: String,
      default: 'pieChart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    chartData: {
      type: Object,
      default: () => ({
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        //右侧工具箱
        toolbox: {
          feature: {
            dataView: { show: true, readOnly: false },
            restore: { show: true },
            saveAsImage: { show: true }
          }
        },
        legend:  {
          left: 'center',
          bottom: '10',
          data: ['数据1', '数据2', '数据3', '数据4', '数据5']
        },
        series: [
          {
            name: '名称',
            type: 'pie',
            roseType: 'radius',
            radius: [15, 95],
            center: ['50%', '38%'],
            data: [
              { value: 5*1000, name: '数据1' },
              { value: 3*1000, name: '数据2' },
              { value: 2*1000, name: '数据3' },
              { value: 4*1000, name: '数据4' },
              { value: 10*1000, name: '数据5' }
            ],
            animationEasing: 'cubicInOut',
            animationDuration: animationDuration
          }
        ]
      })
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions()
      }
    }
  },
  mounted() {

  },
  beforeDestroy() {
    this.destroy()
  },
  methods: {
    /**
     * 初始化图表
     */
    initChart() {
      this.destroy()
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions()
      this.setClick()
    },
    /**
     * 更新图表数据
     */
    setOptions() {
      this.$nextTick(() => {
        this.chart.setOption(this.chartData)
      })
    },
    /**
     * 注册点击事件
     */
    setClick(){
      this.chart.getZr().off("click");
      let that = this
      this.chart.getZr().on("click", function (params) {
        let pointInPixel = [params.offsetX, params.offsetY];
        // 使用containPixel API判断点击位置是否在显示图形区域，下面的例子过滤了绘制图形的网格外的点击事件，好比X、Y轴lable、空白位置等的点击事件。
        if (that.chart.containPixel("grid", pointInPixel)) {
          let xIndex = that.chart.convertFromPixel({ seriesIndex: 0 }, [
            params.offsetX,
            params.offsetY,
          ])[0];
          let op = that.chart.getOption();
          //获取x轴坐标数据
          let xData=op.xAxis[0].data[xIndex]
          //获取所有y轴坐标数据
          let yData=[]
          op.series.forEach(item=>{
            yData.push({
              name:item.name,
              data:item.data[xIndex]
            })
          })
          that.$emit("onclick", xData,yData);
        }
      });
    },
    destroy() {
      if (this.chart) {
        this.chart.dispose()
        this.chart = null
      }
    }
  }
}
</script>
